package com.zheng.current;

import com.zheng.current.dto.Task;
import com.zheng.current.dto.WrapFlightQuery;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * @version 1.0
 * @Copyright (c) 合思技术团队 https://www.hosecloud.com/
 * @Package com.zheng.current
 * @Project：base-test4-mysql-master
 * @authur：zhengtianliang@hosecloud.com
 * @date：2025/6/16 17:39
 * @Desc      测试类
 */
public class CurrentTest {

    public static void main(String[] args) {

        // 创建线程工厂，下面要用
        ThreadFactory threadFactory = r -> {
            Thread thread = new Thread(r,"test-zheng-"+System.currentTimeMillis());
            thread.setDaemon(false);
            return thread;
        };
        // 创建线程池
        ThreadPoolExecutor executor =new ThreadPoolExecutor(40, 40,
                0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(),
                threadFactory,
                new ThreadPoolExecutor.AbortPolicy());
        // 新建公共查询条件，任务那要用
        WrapFlightQuery query = new WrapFlightQuery();
        query.setNonGw(true);

        // 新建任务
        List<ParallelTasker> taskList = new ArrayList<>();
        List<String> channelList = Arrays.asList("CQ","IBE");
        for (String channel : channelList){
            if (Task.IBE.getValue().equals(channel)){
                ParallelTasker tasker = new ParallelTasker(query,Task.IBE);
                taskList.add(tasker);
            }
            if (Task.IBE.getValue().equals(channel)){
                ParallelTasker tasker = new ParallelTasker(query,Task.CQ);
                taskList.add(tasker);
            }

        }

        long start = System.currentTimeMillis();
        try {

            // 执行任务
            List<Future<Map<String, Object>>> futures = executor.invokeAll(taskList, 8, TimeUnit.SECONDS);
            long end = System.currentTimeMillis();
            System.out.println("并发执行完毕了，共耗时："+(end-start)/1000);
        } catch (InterruptedException e) {
            System.out.println("发送从上；"+e);
            throw new RuntimeException(e);
        }catch (Exception e) {
            System.out.println("发送从上111；"+e);
            throw new RuntimeException(e);
        }
        long end = System.currentTimeMillis();
        System.out.println("全部执行完毕了，共耗时："+(end-start)/1000);

    }
}
